//
//  TestNSPredicate.h
//  TestMac
//
//  Created by 小星星吃KFC on 2021/6/7.
//

#import <Foundation/Foundation.h>
#import "Person.h"

static void TestNSPredicate(void){
    
    NSLog(@"\n\n😂⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️TestNSPredicate⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️\n");
    
    NSMutableArray *array = [NSMutableArray array];
    {
        Person *p = Person.alloc.init;
        p.ID = @"1";
        p.name = @"Jim";
        p.age = 2;
        [array addObject:p];
    }
    {
        Person *p = Person.alloc.init;
        p.ID = @"2";
        p.name = @"jim";
        p.age = 1;
        NSMutableArray<Dog *> *dogs = NSMutableArray.array;
        p.dogArray = dogs;
        {
            Dog *dog = Dog.alloc.init;
            [dogs addObject:dog];
            dog.ID = @"2-1";
            dog.name = @"golf";
        }
        
        [array addObject:p];
    }
    {
        Person *p = Person.alloc.init;
        p.ID = @"3";
        p.name = @"tom";
        p.age = 3;
        NSMutableArray<Dog *> *dogs = NSMutableArray.array;
        p.dogArray = dogs;
        {
            Dog *dog = Dog.alloc.init;
            [dogs addObject:dog];
            dog.ID = @"3-1";
            dog.name = @"golf";
        }
        {
            Dog *dog = Dog.alloc.init;
            [dogs addObject:dog];
            dog.ID = @"3-2";
            dog.name = @"golf";
        }
        [array addObject:p];
    }
    {
        Person *p = Person.alloc.init;
        p.ID = @"4";
        p.name = @"tom";
        p.age = 4;
        NSMutableArray<Dog *> *dogs = NSMutableArray.array;
        p.dogArray = dogs;
        {
            Dog *dog = Dog.alloc.init;
            [dogs addObject:dog];
            dog.ID = @"4-1";
            dog.name = @"golf2";
        }
        [array addObject:p];
    }
    {
        Person *p = Person.alloc.init;
        p.ID = @"5";
        p.name = @"joM";
        p.age = 4;
        [array addObject:p];
    }
    
    /// SELF
    /// name
    {
        // endsWith
        // BEGINSWITH
        //            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'm' OR name.length > 4"];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'j'"];
        NSArray *array1 = [array filteredArrayUsingPredicate:predicate];
        NSLog(@"BEGINSWITH=%@",array1);
        
    }
    {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] 'j'"];
        NSArray *array1 = [array filteredArrayUsingPredicate:predicate];
        NSLog(@"BEGINSWITH[cd]=%@",array1);
        
    }
    
    {
        /// 注:[c]不区分大小写 ,
        /// [d]不区分发音符号即没有重音符号 ,
        /// [cd]既不区分大小写，也不区分发音符号
        // 注意这个语法里面字符串要用单引号,或者拼接 %K 代表key %@ 代表value
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name endswith[cd] 'm'"];
        NSArray *array1 = [array filteredArrayUsingPredicate:predicate];
        NSLog(@"endswith=%@",array1);
        
    }
    
    {
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"%K CONTAINS[cd] %@",@"name",@"jim"];
        NSArray *array1 = [array filteredArrayUsingPredicate:pre];
        NSLog(@"ANY name CONTAINS =%@",array1);
    }
    
    {
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@",@"jim"];
        NSArray *array1 = [array filteredArrayUsingPredicate:pre];
        NSLog(@"ANY name CONTAINS =%@",array1);
    }
    
    
    {
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"ANY dogArray.name=='golf'"];
        NSArray *array1 = [array filteredArrayUsingPredicate:pre];
        NSLog(@"ANY=%@",array1);
    }
    {
        /// 必须先判空一下
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"(dogArray != nil) && (dogArray[SIZE] == 2) && (ANY dogArray.name=='golf')"];
        NSArray *array1 = [array filteredArrayUsingPredicate:pre];
        NSLog(@"组合1=%@",array1);
        
        // 比较 NSComparisonPredicate
        // 组合 NSCompoundPredicate and or
        // OR and || &&
        NSPredicate *pre1 = [NSPredicate predicateWithFormat:@"(dogArray != nil)"];
        NSPredicate *pre2 = [NSPredicate predicateWithFormat:@"(dogArray[SIZE] == 2) && (ANY dogArray.name=='golf')"];
        
        NSPredicate *pre3 = [NSCompoundPredicate andPredicateWithSubpredicates:@[pre1,pre2]];
        NSArray *array2 = [array filteredArrayUsingPredicate:pre3];
        NSLog(@"组合2=%@",array2);
        
    }
    
    {
        
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"SOME dogArray.name == 'golf'"];
        NSArray *array1 = [array filteredArrayUsingPredicate:pre];
        NSLog(@"SOME=%@",array1);
        
        
    }
    
    {
        
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"ALL dogArray.name == 'golf'"];
        NSArray *array1 = [array filteredArrayUsingPredicate:pre];
        NSLog(@"ALL=%@",array1);
    }
    {
        
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"(dogArray != nil) && ALL dogArray.name == 'golf'"];
        NSArray *array1 = [array filteredArrayUsingPredicate:pre];
        NSLog(@"ALL=%@",array1);
        
        
    }
    {
        
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"(dogArray != null) && ALL dogArray.name == 'golf'"];
        NSArray *array1 = [array filteredArrayUsingPredicate:pre];
        NSLog(@"ALL=%@",array1);
        
        
    }
    {
        
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"(dogArray != '12') && ALL dogArray.name == 'golf'"];
        NSArray *array1 = [array filteredArrayUsingPredicate:pre];
        NSLog(@"ALL=%@",array1);
        
        
    }
    //    [NSPredicate predicateWithFormat:@"(firstName == %@) || (firstName == nil)"];
    
    
    {
        /// 差集,
        NSArray *filterArray = @[@"ab", @"abc"];
        NSArray *array = @[@"a", @"ab", @"abc", @"abcd"];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)", filterArray];
        NSLog(@"%@", [array filteredArrayUsingPredicate:predicate]);
        
        // (
        //        a,
        //        abcd
        //    )
    }
    
    /**
     // 创建谓词，属性名改为age，要求这个age包含$VALUE字符串
     NSPredicate *predTemp = [NSPredicate predicateWithFormat:@"%K > $VALUE", @"age"];
     // 指定$VALUE的值为 25
     NSPredicate *pred1 = [predTemp predicateWithSubstitutionVariables:@{@"VALUE" : @25}];
     NSArray *newArray1 = [array filteredArrayUsingPredicate:pred1];
     NSLog(@"newArray1:%@", newArray1);
     
     // 修改 $VALUE的值为32
     NSPredicate *pred2 = [predTemp predicateWithSubstitutionVariables:@{@"VALUE" : @32}];
     NSArray *newArray2 = [array filteredArrayUsingPredicate:pred2];
     NSLog(@"newArray2:%@", newArray2);
     */
}
